#include <stdio.h>
#include <stdbool.h>

#define RESULT_VECTOR_LENGTH 4
#define INPUT_VECTOR_LENGTH 4

void cal(double [], double, double);
bool mainCal(int[], double[], int);
void input(double[], int);
void output(int[], int);
int test_succeed(double[]);

int main()
{
    double input_vector[INPUT_VECTOR_LENGTH] = {0};
    input(input_vector, INPUT_VECTOR_LENGTH);
    int signal_vector[INPUT_VECTOR_LENGTH - 1] = {-1, -1, -1};
    char signal_output[RESULT_VECTOR_LENGTH] = {'+', '-', '*', '/'};

    int t;
    for (t = 0; t < INPUT_VECTOR_LENGTH; t++)
        if(mainCal(signal_vector, input_vector, t))
            break;

    if (t == 4)
    {
        printf("No answer for the game......\n");
        return 1;
    }

    for (int j = 0; j < INPUT_VECTOR_LENGTH - 1; j++)
    {
        printf("%.2lf ", input_vector[(t + j) % INPUT_VECTOR_LENGTH]);
        printf("%c ", signal_output[signal_vector[j]]);
    }
    printf("%.2lf\n", input_vector[(t + 3) % INPUT_VECTOR_LENGTH]);
    return 0;
}

void cal(double result_vector[], double a, double b)
{
    result_vector[0] = a + b;
    result_vector[1] = a - b;
    result_vector[2] = a * b;
    result_vector[3] = a / b;
}

void input(double result_vector[], int length)
{
    printf("Input the vector for 24 play_game: ");
    for (int i = 0; i < length; i++)
        scanf(" %lf", &result_vector[i]);
}

void output(int result_vector[], int length)
{
    for(int i = 0; i < length; i++)
        printf("%d ", result_vector[i]);
    printf("\n");
}

int test_succeed(double result_vector[])
{
    for (int i = 0; i < RESULT_VECTOR_LENGTH; i++)
        if (result_vector[i] == 24)
            return i;
    return -1;
}

bool mainCal(int signal_vector[], double input_vector[], int start_index)
{
    double result_vector[RESULT_VECTOR_LENGTH] = {0};
    double a = input_vector[start_index % INPUT_VECTOR_LENGTH];
    double b = input_vector[(start_index + 1) % INPUT_VECTOR_LENGTH];
    double c = input_vector[(start_index + 2) % INPUT_VECTOR_LENGTH];
    double d = input_vector[(start_index + 3) % INPUT_VECTOR_LENGTH];

    cal(result_vector, a, b);
    for (int i = 0; i < RESULT_VECTOR_LENGTH; i++) {
        double result_vector2[RESULT_VECTOR_LENGTH];
        cal(result_vector2, result_vector[i], c);
        for (int j = 0; j < RESULT_VECTOR_LENGTH; j++) {
            double result_vector3[RESULT_VECTOR_LENGTH];
            cal(result_vector3, result_vector2[j], d);
            if (test_succeed(result_vector3) > -1)
            {
                signal_vector[0] = i;
                signal_vector[1] = j;
                signal_vector[2] = test_succeed(result_vector3);
                return true;
            }
        }
    }

    return false;
}

void generate_matrix(double input_matrix[][])
{
    
}